$(function () {
    $("#jqGrid").jqGrid({
        url: '../adverise/queryAll',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'advId', index: 'adv_id', key: true, hidden: true},
            {label: '广告位置', name: 'advPositionName', index: 'adv_position_id', width: 80},
            {label: '广告名称', name: 'advName', index: 'adv_name', width: 80},
            {label: '链接', name: 'advLink', index: 'adv_link', width: 80},
            {
                label: '图片', name: 'advImg', index: 'adv_img', width: 80, formatter: function (value) {
                return transImg(value);
            }
            },
            /*{label: '内容', name: 'content', index: 'content', width: 80},*/
          /*  {
                label: '结束时间', name: 'endTime', index: 'end_time', width: 80, formatter: function (value) {
                return transDate(value);
            }
            },*/
            {
                label: '状态', name: 'advStatus', index: 'adv_status', width: 80, formatter: function (value) {
                if(value == 0){
                    return  '<span class="label label-danger">禁用</span>'
                }
                if(value == 1){
                    return  '<span class="label label-success">正常</span>';
                }
                return '<span class="label label-success">已删除</span>';
            }
            }],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.pageNum",
            total: "page.pages",
            records: "page.total"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        ad: {advStatus: 1, advImg: ''},
        ruleValidate: {
            advName: [
                {required: true, message: '广告名称不能为空', trigger: 'blur'}
            ]/*,
            advImg: [
                {required: true, message: '图片不能为空', trigger: 'blur'}
            ]*/
        },
        q: {
            name: ''
        },
        advPositions: [],
        advPositionId_1:'',

    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.ad = {advStatus: 1, advImg: '',advPositionId:''};
            vm.advPositions = [];
            this.getAdvPositions();
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            this.getInfo(id);
            this.getAdvPositions();


        },
        saveOrUpdate: function (event) {
           /* var url = vm.ad.id == null ? "../adverise/save" : "../adverise/update";*/
           var url = "../adverise/saveOrUpdate";
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json",
                data: JSON.stringify(vm.ad),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: "../adverise/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get("../adverise/info/" + id, function (r) {
                vm.ad = r.advertise;
                //vm.getAdvPositions();
            });
        },
        /**
         * 获取广告位置
         */
        getAdvPositions: function () {
            $.get("../advposition/queryAll", function (r) {
                vm.advPositions = r.page.list;
                //console.log("advPositions"+vm.advPositions)
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        handleSuccess: function (res, file) {
            vm.ad.advImg = file.response.url;
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
            });
        },
        handleMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
            });
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        eyeImage: function () {
            var url = vm.ad.advImg;
            eyeImage(url);
        }
    }
});